//
//  WYTestHeader.m
//  JinMiTong
//
//  Created by admin on 2016/11/24.
//  Copyright © 2016年 com.liji. All rights reserved.
//

#import "WYTestHeader.h"
#define pi 3.14159265359
#define DEGREES_TO_RADIANS(degress) ((pi * degress)/180)
@interface WYTestHeader()
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UISwitch *s;
@property (weak, nonatomic) UIImageView *logo;
@property (weak, nonatomic) UIActivityIndicatorView *loading;
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong)CABasicAnimation *animation;
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)CALayer *wlayer;
@property (nonatomic,strong)CABasicAnimation* ano;
@end
@implementation WYTestHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"JJJJ"]];
    logo.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logo];
    self.logo = logo;
    
    CALayer *testLayer = [CALayer layer];
    testLayer.backgroundColor = [UIColor clearColor].CGColor;
    [self.logo.layer addSublayer:testLayer];
    self.wlayer=testLayer;
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.strokeColor = [UIColor redColor].CGColor;
    _shapeLayer.lineCap = kCALineCapRound;
    _shapeLayer.lineWidth = 1;
    [self.wlayer addSublayer:_shapeLayer];
    UIBezierPath *thePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:20-3.5 startAngle:0 endAngle:DEGREES_TO_RADIANS(320) clockwise:YES];
    _shapeLayer.path = thePath.CGPath;
    CABasicAnimation *animation = [CABasicAnimation animation];
    self.ano=animation;
    animation.keyPath = @"transform.rotation.z";
    animation.duration = 0.8f;
    animation.fromValue = @(0);
    animation.toValue = @(2*M_PI);
    animation.repeatCount = INFINITY;
    //[self.wlayer addAnimation:animation forKey:nil];
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    self.logo.frame=CGRectMake(kScreenWidth/2-20, 7,40, 40);
    self.wlayer.frame =CGRectMake(0, 0,40, 40);

}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}
#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:{
            self.logo.hidden=YES;
            [self.wlayer removeAllAnimations];
        }
            break;
        case MJRefreshStatePulling:
        {
            self.logo.hidden=NO;
        }
            
            break;
        case MJRefreshStateRefreshing:
        {
            self.logo.hidden=NO;
            [self.wlayer addAnimation:self.ano forKey:nil];

            
        }
            break;
        default:
            break;
    }
}
#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    // 1.0 0.5 0.0
    // 0.5 0.0 0.5
    CGFloat red = 1.0 - pullingPercent * 0.5;
    CGFloat green = 0.5 - 0.5 * pullingPercent;
    CGFloat blue = 0.5 * pullingPercent;
    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
-(void)anitamal
{
    CALayer *testLayer = [CALayer layer];
    testLayer.backgroundColor = [UIColor clearColor].CGColor;
    testLayer.frame = CGRectMake(0, 0, 100, 100);
    [self.imageView.layer addSublayer:testLayer];
    
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.strokeColor = [UIColor redColor].CGColor;
    _shapeLayer.lineCap = kCALineCapRound;
    _shapeLayer.lineWidth = 3;
    
    UIBezierPath *thePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50-3.5 startAngle:0 endAngle:DEGREES_TO_RADIANS(330) clockwise:YES];
    _shapeLayer.path = thePath.CGPath;
    [testLayer addSublayer:_shapeLayer];
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation.z";
    animation.duration = 0.8f;
    animation.fromValue = @(0);
    animation.toValue = @(2*M_PI);
    animation.repeatCount = INFINITY;
    
    [testLayer addAnimation:animation forKey:nil];
}
-(void)stop
{
    [self.imageView removeFromSuperview];
}





@end
